package com.coffee.daili.service.impl;

import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import com.coffee.daili.api.query.DailiMerchantQuotaLogQuery;
import com.coffee.daili.api.result.DailiMerchantQuotaLogResult;
import com.coffee.daili.api.service.DailiMerchantQuotaLogService;
import com.coffee.daili.service.bean.DailiMerchantQuotaLog;
import com.coffee.daili.service.dao.DailiMerchantQuotaLogDao;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class DailiMerchantQuotaLogServiceImpl implements DailiMerchantQuotaLogService {
	@Resource
	private DailiMerchantQuotaLogDao	dailiMerchantQuotaLogDao;

	@Override
	public Page<DailiMerchantQuotaLogResult> getPager(DailiMerchantQuotaLogQuery param, PagerInfo pr) {
		Page<DailiMerchantQuotaLogResult> pager=new Page<DailiMerchantQuotaLogResult>(pr.getStart(), pr.getPageSize());
		Integer count=dailiMerchantQuotaLogDao.count(param);
		pager.setTotalPage(count);
		if(count>0) {
			List<DailiMerchantQuotaLog> list=dailiMerchantQuotaLogDao.page(param, pr.getStart(), pr.getPageSize());
			pager.setData(DTOUtils.convertList(list, DailiMerchantQuotaLogResult.class));
		}
		return pager;
	}
	
	@Override
	public Integer count(DailiMerchantQuotaLogQuery param){
		Integer count=dailiMerchantQuotaLogDao.count(param);
		return count;
	}
	
	@Override
	public DailiMerchantQuotaLogResult getDetail(Integer id){
		DailiMerchantQuotaLog item=dailiMerchantQuotaLogDao.findById(id);
		if(item==null){
			return null;
		}
		DailiMerchantQuotaLogResult result=new DailiMerchantQuotaLogResult();
		BeanUtilExt.copyProperties(result, item);
		
		return result;
	}

	@Override
	public List<DailiMerchantQuotaLogResult> getList(DailiMerchantQuotaLogQuery param, PagerInfo pr){
		List<DailiMerchantQuotaLog> list=dailiMerchantQuotaLogDao.page(param, pr.getStart(), pr.getPageSize());
		return DTOUtils.convertList(list, DailiMerchantQuotaLogResult.class);
	}
	
	@Override
	public List<DailiMerchantQuotaLogResult> getList(DailiMerchantQuotaLogQuery param){
		List<DailiMerchantQuotaLog> list=dailiMerchantQuotaLogDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, DailiMerchantQuotaLogResult.class);
	}
	
	@Override
	public List<DailiMerchantQuotaLogResult> getList(){
		DailiMerchantQuotaLogQuery param = new DailiMerchantQuotaLogQuery();
		List<DailiMerchantQuotaLog> list=dailiMerchantQuotaLogDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, DailiMerchantQuotaLogResult.class);
	}


	@Override
	public void delete(Integer id) {
		dailiMerchantQuotaLogDao.deleteById(id);
	}

	@Override
	public void addOrUpdate(DailiMerchantQuotaLogResult result)throws BusinessException {
		DailiMerchantQuotaLog item=new DailiMerchantQuotaLog();
		BeanUtilExt.copyProperties(item, result);
		if(item.getId()==null) {
			item.setCreateTime(new Date());
			dailiMerchantQuotaLogDao.insert(item);
		}else {
			DailiMerchantQuotaLog tmp=dailiMerchantQuotaLogDao.findById(item.getId());
			if(tmp==null) {
				throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
			}
			dailiMerchantQuotaLogDao.updateById(item);
		}
	}
	
	@Override
	public Map<Integer,DailiMerchantQuotaLogResult> getItemMap(Set<Integer> idSet){
		Map<Integer, DailiMerchantQuotaLogResult> map=new HashMap<Integer, DailiMerchantQuotaLogResult>();
		List<DailiMerchantQuotaLog> list=dailiMerchantQuotaLogDao.findByIds(StringUtil.setToList(idSet));
		List<DailiMerchantQuotaLogResult> resultList=DTOUtils.convertList(list, DailiMerchantQuotaLogResult.class);
		for(DailiMerchantQuotaLogResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}
	
	@Override
	public Map<Integer,DailiMerchantQuotaLogResult> getItemMap(){
		Map<Integer, DailiMerchantQuotaLogResult> map=new HashMap<Integer, DailiMerchantQuotaLogResult>();
		DailiMerchantQuotaLogQuery param=new DailiMerchantQuotaLogQuery();
		List<DailiMerchantQuotaLog> list=dailiMerchantQuotaLogDao.page(param, 0, Integer.MAX_VALUE);
		List<DailiMerchantQuotaLogResult> resultList=DTOUtils.convertList(list, DailiMerchantQuotaLogResult.class);
		for(DailiMerchantQuotaLogResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}	
	
	
	
}
